
(define example-file "/home/rohan/uc/uc-13/midi/_L_OGOS.midi")

;; Locate and print out any set tempo events.

(let ((data (midi-file-read-file example-file)))
  (inform "Format Type:" (midi-file-format-type data))
  (inform "Number of tracks:" (midi-file-number-of-tracks data))
  (inform "Division:" (midi-file-division data))
  (for-each (lambda (e)
	      (if (midi-meta-event-set-tempo? (second e))
		  (inform "Set Tempo Meta Event:" 
			  (decode-midi-meta-event-set-tempo (second e)))))
	    (midi-file-track data 0)))

;; Read a MIDI file and write out to a temporary file.

(let ((input-port (open-input-file example-file))
      (output-port (open-output-file* "/tmp/TEST.midi")))
  (midi-file-write-file 
   (midi-file-read-file example-file)
   "/tmp/TEST.midi"))

;; Check that the two files parse to identical lisp structures.

(let ((data-a (midi-file-read-file example-file))
      (data-b (midi-file-read-file "/tmp/TEST.midi")))
  (if (not (equal? data-a data-b))
      (error "Oh dear, lisp data is not equal...")
      (inform "Lisp Data is Equal!!!!")))

(midi-file-read-file example-file)

(midi-file-read-file "/tmp/TEST.midi")

;; Test SysEx handling.

(let ((send '((0 1 1024)
	      ((0 (#xF0 16 16 16 #xF7))))))
  (midi-file-write-file send "/tmp/TEST.midi")
  (let ((recv (midi-file-read-file "/tmp/TEST.midi")))
    (inform "Data equal?" (equal? send recv))
    recv))

;; Test variable-length integer procedures. (Internal definitions.)

(define (test-variable-length-integers z)
  (let ((o (open-output-string)))
    (write-variable-length-integer z o)
    (let* ((s (get-output-string o))
	   (l (map char->integer (string->list s)))
	   (i (open-input-string s)))
      (list z l (read-variable-length-integer i)))))

(map test-variable-length-integers '(0 65 131 6780)) 

; => ((0 (0) 0) (65 (65) 65) (131 (129 3) 131) (6780 (180 124) 6780))
